package cn.threewaters.qtyy.web.facade;

import java.util.Iterator;
import java.util.List;

import com.google.common.collect.Lists;

public class TempAnaly {

	public Point[][][] analysis(String tempdata, int cablehang, int cablelie, int cableceng, String stapoint,
			String sortdir, String pointrule, String sortrule) {
		List<Point> resultList = Lists.newArrayList();

		String[] tempList = tempdata.split("\\$");
		for (String temp : tempList) {
			if (temp != null && !temp.equals("")) {
				if (temp.split(":")[0] != null && !temp.split(":")[0].equals("0")) {
					Point point = new Point(0, 0, 0, temp.split(":")[0], temp.split(":")[2]);
					resultList.add(point);
				}
			}
		}

		Point[][][] result = new Point[cablehang][cablelie][cableceng];
		Iterator<Point> iterator = resultList.iterator();
		if (stapoint != null && stapoint.equals("0")) {// 左上
			// 横向
			if (sortdir != null && sortdir.equals("0")) {
				zuoshangheng(cablehang, cablelie, cableceng, pointrule, sortrule, result, iterator);
			}
			// 纵向
			if (sortdir != null && sortdir.equals("1")) {
				zuoshangzong(cablehang, cablelie, cableceng, pointrule, sortrule, result, iterator);
			}
		}
		if (stapoint != null && stapoint.equals("1")) {// 左下
			// 横向
			if (sortdir != null && sortdir.equals("0")) {
				zuoxiaheng(cablehang, cablelie, cableceng, pointrule, sortrule, result, iterator);
			}
			// 纵向
			if (sortdir != null && sortdir.equals("1")) {
				zuoxiazong(cablehang, cablelie, cableceng, pointrule, sortrule, result, iterator);
			}
		}
		if (stapoint != null && stapoint.equals("2")) {// 右上
			// 横向
			if (sortdir != null && sortdir.equals("0")) {
				youshangheng(cablehang, cablelie, cableceng, pointrule, sortrule, result, iterator);
			}
			// 纵向
			if (sortdir != null && sortdir.equals("1")) {
				youshangzong(cablehang, cablelie, cableceng, pointrule, sortrule, result, iterator);
			}
		}
		if (stapoint != null && stapoint.equals("3")) {// 右下
			// 横向
			if (sortdir != null && sortdir.equals("0")) {
				youxiaheng(cablehang, cablelie, cableceng, pointrule, sortrule, result, iterator);
			}
			// 纵向
			if (sortdir != null && sortdir.equals("1")) {
				youxiazong(cablehang, cablelie, cableceng, pointrule, sortrule, result, iterator);
			}
		}
		for (int i = 0; i < result.length; i++) {
			for (int j = 0; j < result[i].length; j++) {
				for (int k = 0; k < result[i][j].length; k++) {
					if (result[i][j][k] != null) {
						result[i][j][k].setY(k + 1);
						result[i][j][k].setX(j + 1);
						result[i][j][k].setZ(i + 1);
					} else {
						result[i][j][k] = new Point();
					}

				}
			}
		}
		return result;
	}

	// 左上纵 已完成
	private void zuoshangzong(int cablehang, int cablelie, int cableceng, String pointrule, String sortrule,
			Point[][][] result, Iterator<Point> iterator) {
		for (int j = 0; j < cablelie; j++) {
			// S型
			if (sortrule != null && sortrule.equals("0")) {
				if ((j + 1) % 2 == 1) {
					for (int i = 0; i < cablehang; i++) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				} else {
					for (int i = cablehang - 1; i >= 0; i--) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				}
			}
			// E型
			if (sortrule != null && sortrule.equals("1")) {
				for (int i = 0; i < cablehang; i++) {
					// 正序
					if (pointrule != null && pointrule.equals("0")) {
						for (int k = 0; k < cableceng; k++) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
					// 逆序
					if (pointrule != null && pointrule.equals("1")) {
						for (int k = cableceng - 1; k >= 0; k--) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
				}
			}
		}
	}

	// 左上横 已完成
	private void zuoshangheng(int cablehang, int cablelie, int cableceng, String pointrule, String sortrule,
			Point[][][] result, Iterator<Point> iterator) {
		for (int i = 0; i < cablehang; i++) {
			// S型
			if (sortrule != null && sortrule.equals("0")) {
				if ((i + 1) % 2 == 1) {
					for (int j = 0; j < cablelie; j++) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				} else {
					for (int j = cablelie - 1; j >= 0; j--) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				}
			}
			// E型
			if (sortrule != null && sortrule.equals("1")) {
				for (int j = 0; j < cablelie; j++) {
					// 正序
					if (pointrule != null && pointrule.equals("0")) {
						for (int k = 0; k < cableceng; k++) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
					// 逆序
					if (pointrule != null && pointrule.equals("1")) {
						for (int k = cableceng - 1; k >= 0; k--) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
				}
			}
		}
	}

	// 左下纵 已完成
	private void zuoxiazong(int cablehang, int cablelie, int cableceng, String pointrule, String sortrule,
			Point[][][] result, Iterator<Point> iterator) {
		for (int j = 0; j < cablelie; j++) {
			// S型
			if (sortrule != null && sortrule.equals("0")) {
				if ((j + 1) % 2 != 1) {
					for (int i = cablehang - 1; i >= 0; i--) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				} else {
					for (int i = 0; i < cablehang; i++) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				}
			}
			// E型
			if (sortrule != null && sortrule.equals("1")) {
				for (int i = 0; i < cablehang; i++) {
					// 正序
					if (pointrule != null && pointrule.equals("0")) {
						for (int k = 0; k < cableceng; k++) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
					// 逆序
					if (pointrule != null && pointrule.equals("1")) {
						for (int k = cableceng - 1; k >= 0; k--) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
				}
			}
		}
	}

	// 左下横 已完成
	private void zuoxiaheng(int cablehang, int cablelie, int cableceng, String pointrule, String sortrule,
			Point[][][] result, Iterator<Point> iterator) {
		for (int i = cablehang - 1; i >= 0; i--) {
			// S型
			if (sortrule != null && sortrule.equals("0")) {
				if ((i + 1) % 2 == (cablehang % 2)) {
					for (int j = 0; j < cablelie; j++) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				} else {
					for (int j = cablelie - 1; j >= 0; j--) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				}
			}
			// E型
			if (sortrule != null && sortrule.equals("1")) {
				for (int j = 0; j < cablelie; j++) {
					// 正序
					if (pointrule != null && pointrule.equals("0")) {
						for (int k = 0; k < cableceng; k++) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
					// 逆序
					if (pointrule != null && pointrule.equals("1")) {
						for (int k = cableceng - 1; k >= 0; k--) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
				}
			}
		}
	}

	// 右上纵 已完成
	private void youshangzong(int cablehang, int cablelie, int cableceng, String pointrule, String sortrule,
			Point[][][] result, Iterator<Point> iterator) {
		for (int j = cablelie - 1; j >= 0; j--) {
			// S型
			if (sortrule != null && sortrule.equals("0")) {
				if ((j + 1) % 2 == cablelie % 2) {
					for (int i = 0; i < cablehang; i++) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				} else {
					for (int i = cablehang - 1; i >= 0; i--) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				}
			}
			// E型
			if (sortrule != null && sortrule.equals("1")) {
				for (int i = 0; i < cablehang; i++) {
					// 正序
					if (pointrule != null && pointrule.equals("0")) {
						for (int k = 0; k < cableceng; k++) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
					// 逆序
					if (pointrule != null && pointrule.equals("1")) {
						for (int k = cableceng - 1; k >= 0; k--) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
				}
			}
		}
	}

	// 右上横 已完成
	private void youshangheng(int cablehang, int cablelie, int cableceng, String pointrule, String sortrule,
			Point[][][] result, Iterator<Point> iterator) {
		for (int i = 0; i < cablehang; i++) {
			// S型
			if (sortrule != null && sortrule.equals("0")) {
				if ((i + 1) % 2 == 1) {
					for (int j = cablelie - 1; j >= 0; j--) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				} else {
					for (int j = 0; j < cablelie; j++) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				}
			}
			// E型
			if (sortrule != null && sortrule.equals("1")) {
				for (int j = cablelie - 1; j >= 0; j--) {
					// 正序
					if (pointrule != null && pointrule.equals("0")) {
						for (int k = 0; k < cableceng; k++) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
					// 逆序
					if (pointrule != null && pointrule.equals("1")) {
						for (int k = cableceng - 1; k >= 0; k--) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
				}
			}
		}
	}

	// 右下纵 已完成
	private void youxiazong(int cablehang, int cablelie, int cableceng, String pointrule, String sortrule,
			Point[][][] result, Iterator<Point> iterator) {
		for (int j = cablelie - 1; j >= 0; j--) {
			// S型
			if (sortrule != null && sortrule.equals("0")) {
				if ((j + 1) % 2 == cablelie % 2) {
					for (int i = cablehang - 1; i >= 0; i--) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				} else {
					for (int i = 0; i < cablehang; i++) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				}
			}
			// E型
			if (sortrule != null && sortrule.equals("1")) {
				for (int i = cablehang - 1; i >= 0; i--) {
					// 正序
					if (pointrule != null && pointrule.equals("0")) {
						for (int k = 0; k < cableceng; k++) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
					// 逆序
					if (pointrule != null && pointrule.equals("1")) {
						for (int k = cableceng - 1; k >= 0; k--) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
				}
			}
		}
	}

	// 右下横 已完成
	private void youxiaheng(int cablehang, int cablelie, int cableceng, String pointrule, String sortrule,
			Point[][][] result, Iterator<Point> iterator) {
		for (int i = cablehang - 1; i >= 0; i--) {
			// S型
			if (sortrule != null && sortrule.equals("0")) {
				if ((i + 1) % 2 == (cablehang % 2)) {
					for (int j = cablelie - 1; j >= 0; j--) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				} else {
					for (int j = 0; j < cablelie; j++) {
						// 正序
						if (pointrule != null && pointrule.equals("0")) {
							for (int k = 0; k < cableceng; k++) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
						// 逆序
						if (pointrule != null && pointrule.equals("1")) {
							for (int k = cableceng - 1; k >= 0; k--) {
								if (iterator.hasNext()) {
									result[i][j][k] = iterator.next();
								}
							}
						}
					}
				}
			}
			// E型
			if (sortrule != null && sortrule.equals("1")) {
				for (int j = cablelie - 1; j >= 0; j--) {
					// 正序
					if (pointrule != null && pointrule.equals("0")) {
						for (int k = 0; k < cableceng; k++) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
					// 逆序
					if (pointrule != null && pointrule.equals("1")) {
						for (int k = cableceng - 1; k >= 0; k--) {
							if (iterator.hasNext()) {
								result[i][j][k] = iterator.next();
							}
						}
					}
				}
			}
		}
	}

	public Point[][][] createTestData() {
		int cablehang = 7;
		int cablelie = 13;
		int cableceng = 5;
		StringBuilder tempdata = new StringBuilder();
		for (int i = 1; i <= cablehang * cablelie; i++) {
			for (int j = 1; j <= cableceng; j++) {
				tempdata.append("$");
				tempdata.append(i + ":" + j + ":" + i + "." + j);
			}
		}
		System.out.println(tempdata.toString());

		String stapoint = "0"; // 起始点：0左上，1左下，2右上，3右下
		String sortdir = "0"; // 排序方向：0横，1纵
		String sortrule = "0"; // 排序规则：0S,1E
		String pointrule = "0"; // 层号规则：0从上到下，1从下到上

		Point[][][] analysis = this.analysis(tempdata.toString(), cablehang, cablelie, cableceng, stapoint, sortdir,
				pointrule, sortrule);
		return analysis;
	}

	public static void main(String[] args) {
		TempAnaly tempAnaly = new TempAnaly();
		int cablehang = 7;
		int cablelie = 13;
		int cableceng = 5;
		StringBuilder tempdata = new StringBuilder();
		for (int i = 1; i <= cablehang * cablelie; i++) {
			for (int j = 1; j <= cableceng; j++) {
				tempdata.append("$");
				tempdata.append(i + ":" + j + ":" + i + "." + j);
			}
		}
		System.out.println(tempdata.toString());

		String stapoint = "0"; // 起始点：0左上，1左下，2右上，3右下
		String sortdir = "0"; // 排序方向：0横，1纵
		String sortrule = "0"; // 排序规则：0S,1E
		String pointrule = "0"; // 层号规则：0从上到下，1从下到上

		Point[][][] analysis = tempAnaly.analysis(tempdata.toString(), cablehang, cablelie, cableceng, stapoint,
				sortdir, pointrule, sortrule);

		for (int i = 0; i < analysis.length; i++) {
			for (int j = 0; j < analysis[i].length; j++) {
				for (int k = 0; k < analysis[i][j].length; k++) {
					if (k == 0) {
						System.out.print(analysis[i][j][k] + "  ");
					}
				}
			}
			System.out.println("");
		}
	}
}
